from django.db import models
from django.utils.timezone import now


class Book(models.Model):
    name = models.CharField(max_length=100, help_text='图书名称')
    author = models.CharField(max_length=20, help_text='作者')
    pub_time = models.DateTimeField(default=now, help_text='发布时间')
    price = models.FloatField(default=0, help_text='价格')
    create_time = models.DateTimeField(auto_now_add=True, help_text='创建时间')
    update_time = models.DateTimeField(auto_now=True, help_text='更新时间')

    class Meta:
        # 数据库表名
        db_table = 'book'
        # 指定排序方式
        ordering = ('-pub_time', '-id')

    def __str__(self):
        return f'<Book:({self.id}, {self.name}, {self.author}, {self.price}, {self.pub_time})>'

    __repr__ = __str__


class Publisher(models.Model):
    name = models.CharField(max_length=100, help_text='出版社名称')
    address = models.CharField(max_length=150, help_text='出版社地址')

    def __str__(self):
        return f'<Publisher:({self.name}, {self.address})>'

    __repr__ = __str__
